package com.md24housecall.activity;

import com.md24housecall.iab.util.IabHelper;
import com.md24housecall.iab.util.IabResult;
import com.md24housecall.iab.util.Inventory;
import com.md24housecall.iab.util.Purchase;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainPurchaseActivity extends Activity {
	protected IabHelper mHelper;
	protected GlobalConstant constant = GlobalConstant.getInstance();
	/*
	 *android.test.purchased:request with this product ID, Google Play responds as though you successfully purchased an item.
	 *android.test.canceled:request with this product ID Google Play responds as though the purchase was canceled.
	 *android.test.refunded:request with this product ID, Google Play responds as though the purchase was refunded.
	 *android.test.item_unavailable:request with this product ID, Google Play responds as though the item being purchased was not listed in your application's product list.
	 */
	protected final String ITEM_SKU = "android.test.purchased";
	protected final int REQUESTPURCHASECODE=10001;
	
	protected IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {

		@Override
		public void onIabPurchaseFinished(IabResult result, Purchase info) {
			// TODO Auto-generated method stub
			if (result.isFailure()) {
				// Handle error
				return;
			} else if (info.getSku().equals(ITEM_SKU)) {
				consumeItem();

			}
		}
	};
	private IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {

		@Override
		public void onQueryInventoryFinished(IabResult result,
				Inventory inventory) {
			// TODO Auto-generated method stub
			if (result.isFailure()) {
				// Handle failure
			} else {
				mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
						mConsumeFinishedListener);
			}
		}
	};

	private IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {

		@Override
		public void onConsumeFinished(Purchase purchase, IabResult result) {
			// TODO Auto-generated method stub
			onConsumeCallBack(purchase, result);
		}
	};
	
	protected void onConsumeCallBack(Purchase purchase, IabResult result){
		if (result.isSuccess()) {
			// generate code from WS
		} else {
			// handle error
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		String base64EncodedPublicKey = constant.GOOGLEPLAYKEY;

		mHelper = new IabHelper(this, base64EncodedPublicKey);
		mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {

			@Override
			public void onIabSetupFinished(IabResult result) {
				// TODO Auto-generated method stub
				if (!result.isSuccess()) {
					MyLog.d(GlobalConstant.TAG, "In-app Billing setup failed: "
							+ result);
				} else {
					MyLog.d(GlobalConstant.TAG, "In-app Billing is set up OK");
				}
			}

		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
			super.onActivityResult(requestCode, resultCode, data);
		}
	}

	public void consumeItem() {
		mHelper.queryInventoryAsync(mReceivedInventoryListener);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mHelper != null)
			mHelper.dispose();
		mHelper = null;
	}
}
